package com.zhiyou.controller.LesseeController;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhiyou.bean.Lessee;

import com.zhiyou.bean.Logistics;
import com.zhiyou.service.SystemLesseeService.LesseeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/lessee")
public class LesseeController {
    @Autowired
    LesseeService lesseeService;

    @RequestMapping("/list")
    public String getAll(@RequestParam(value = "pn" ,defaultValue = "1") Integer pn, Map<String , PageInfo> map){
        PageHelper.startPage(pn,5);
        List<Lessee> lessee = lesseeService.getAll();
        PageInfo pageInfo = new PageInfo(lessee,5);
        map.put("pageInfo",pageInfo);

        return "lesseelist";
    }
    @RequestMapping("/detail")
    public String detail(String lId, Map<String , Lessee> map){
        int id = Integer.parseInt(lId);
        Lessee lessee= lesseeService.detail(id);
        System.out.println(lessee);
        map.put("lessees",lessee);
        return "lesseedetail";
    }
    @RequestMapping("/find")
    public String find(String lId, Map<String ,Lessee> map){
        int id = Integer.parseInt(lId);
        Lessee lessee = lesseeService.detail(id);
        map.put("lessee",lessee);
        return "lesseeedit";
    }

    @RequestMapping("/edit")
    public String edit(Lessee lessee,@RequestParam(value = "pn" ,defaultValue = "1") Integer pn, Map<String ,PageInfo> map){
        System.out.println(lessee.toString());
        int i = lesseeService.update(lessee);
        if (i==1){
            PageHelper.startPage(pn,20);
            List<Lessee> list = lesseeService.getAll();
            PageInfo pageInfo = new PageInfo(list,5);
            map.put("pageInfo",pageInfo);
            return "lesseelist";
        }
        return "lesseeedit";
    }
    @RequestMapping("/delete")
    public String delete(String lId,@RequestParam(value = "pn" ,defaultValue = "1") Integer pn, Map<String ,PageInfo> map){
        int id = Integer.parseInt(lId);
        int i = lesseeService.deleteOne(id);
        if (i >= 1){
            PageHelper.startPage(pn,20);
            List<Lessee> list = lesseeService.getAll();
            PageInfo pageInfo = new PageInfo(list,5);
            map.put("pageInfo",pageInfo);
            return "lesseelist";
        }
        return "failed";
    }
    @RequestMapping("/kong")
    public String kong(){
        return "lesseeadd";
    }

    @RequestMapping("/add")
    public String addOne(Lessee lessee,@RequestParam(value = "pn" ,defaultValue = "1") Integer pn, Map<String ,PageInfo> map){
        int i = lesseeService.addOne(lessee);
        if (i >= 1){
            PageHelper.startPage(pn,20);
            List<Lessee> list = lesseeService.getAll();
            PageInfo pageInfo = new PageInfo(list,5);
            map.put("pageInfo",pageInfo);
            return "lesseelist";
        }
        return "lesseeadd";
    }

    @RequestMapping("/search")
    public String search(String field, String keyword, @RequestParam(value = "pn" ,defaultValue = "1") Integer pn, Map<String ,PageInfo> map, HttpSession session){
        System.out.println("field = " + field + ",keyword = " + keyword);
        PageHelper.startPage(pn,20);

        if (keyword == ""){
            session.removeAttribute("field");
            session.removeAttribute("keyword");
            PageHelper.startPage(pn,5);
            List<Lessee> list = lesseeService.getAll();
            PageInfo pageInfo = new PageInfo(list,5);
            map.put("pageInfo",pageInfo);
        }
        session.setAttribute("field",field);
        session.setAttribute("keyword",keyword);
        PageHelper.startPage(pn,5);
        List<Lessee> list = lesseeService.search(field, keyword);
        PageInfo pageInfo = new PageInfo(list,5);
        map.put("pageInfo",pageInfo);
        return "lesseelist2";
    }
}
